<?php
/**
 * mobile公共方法
 *
 * 公共方法
 *
 * @好商城 (c) 2015-2018 33HAO Inc. (http://www.33hao.com)
 * @license    http://www.33 hao.c om
 * @link       交流群号：138182377
 * @since      好商城提供技术支持 授权请购买shopnc授权
 */
defined('In33hao') or exit('Access Invalid!');

function output_data($datas, $extend_data = array(), $error = false) {
    $data = array();
    $data['code'] = 200;
    if($error) {
        $data['code'] = 400;
    }
    if(!empty($extend_data)) {
        $data = array_merge($data, $extend_data);
    }
    $data['datas'] = $datas;
    $jsonFlag = 0 && C('debug') && version_compare(PHP_VERSION, '5.4.0') >= 0
        ? JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
        : 0;
    if ($jsonFlag) {
        header('Content-type: text/plain; charset=utf-8');
    }

    if (!empty($_GET['callback'])) {
        echo $_GET['callback'].'('.json_encode($data, $jsonFlag).')';die;
    } else {
        header("Access-Control-Allow-Origin:*");
        echo json_encode($data, $jsonFlag);die;
    }
}

function output_error($message, $extend_data = array()) {
    $datas = array('error' => $message);
    output_data($datas, $extend_data, true);
}

function mobile_page($page_count) {
    //输出是否有下一页
    $extend_data = array();
    $current_page = intval($_GET['curpage']);
    if($current_page <= 0) {
        $current_page = 1;
    }
    if($current_page >= $page_count) {
        $extend_data['hasmore'] = false;
    } else {
        $extend_data['hasmore'] = true;
    }
    $extend_data['page_total'] = $page_count;
    return $extend_data;
}

function get_server_ip() {
    if (isset($_SERVER)) {
        if($_SERVER['SERVER_ADDR']) {
            $server_ip = $_SERVER['SERVER_ADDR'];
        } else {
            $server_ip = $_SERVER['LOCAL_ADDR'];
        }
    } else {
        $server_ip = getenv('SERVER_ADDR');
    }
    return $server_ip;
}

function http_get($url) {
    return file_get_contents($url);
}

function http_post($url, $param) {
    $postdata = http_build_query($param);

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    return @file_get_contents($url, false, $context);
}

function http_postdata($url, $postdata) {
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    return @file_get_contents($url, false, $context);
}

function getTime($page)
{
    $thismonth = date('m')+$page-1-1;
    $thisyear = date('Y');
    $startDay = $thisyear . '-' . $thismonth . '-1';
    $endDay = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay));
    $data['startMonth']  = strtotime($startDay);//当前月的月初时间戳
    $data['endMonth'] = strtotime($endDay) + 86399;//当前月的月末时间戳
    $res['time_area'] = implode(',',$data);
    $res['year'] = $thisyear;
    $res['month'] = $thismonth;
    $res['startMonth'] = $data['startMonth'];
    $res['endMonth'] = $data['endMonth'];
    return $res;
}

/**
 * @param $year  给定的年份
 * @param $month 给定的月份
 * @param $legth 筛选的区间长度 取前六个月就输入6
 * @param int $page 分页
 * @return array
 */
function getLastTimeArea($year,$month,$legth,$page=1)
{
    if (!$page) {
        $page = 1;
    }
    $monthNum = $month + $legth - $page*$legth;
    $num = 1;
    if ($monthNum < -12) {
        $num = ceil($monthNum/(-12));
    }
    $timeAreaList = [];
    for($i=0;$i<$legth;$i++) {
        $temMonth = $monthNum - $i;
        $temYear = $year;
        if ($temMonth <= 0) {
            $temYear = $year - $num;
            $temMonth = $temMonth + 12*$num;
            if ($temMonth <= 0) {
                $temMonth += 12;
                $temYear -= 1;
            }
        }
        $startMonth  = strtotime($temYear.'/'.$temMonth.'/01');//该月的月初时间戳
        $endMonth = strtotime($temYear.'/'.($temMonth + 1).'/01') - 1;//该月的月末时间戳
        $res['date'] = $temYear.'年'.$temMonth.'月'; //该月的月初格式化时间
        $res['startMonth'] = $temYear.'/'.$temMonth.'/01'; //该月的月初格式化时间
        $res['endMonth'] = date('Y/m/d',$endMonth);//该月的月末格式化时间
        $res['timeArea'] = implode(',',[$startMonth, $endMonth]);//区间时间戳
        $timeAreaList[] = $res;
    }
    return $timeAreaList;
}